package com.example.bomc.controller;


import com.example.bomc.common.msg.CodeMsg;
import com.example.bomc.model.FixStaff;
import com.example.bomc.service.fixStaffService.FixStaffService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;

@Controller
public class AdminController {
    private static final Logger logger = LoggerFactory.getLogger(LoginController.class);


    @Autowired
    FixStaffService fixStaffService;


    /*
     * 审批用户注册
     * */
    @RequestMapping(path = {"/examined/"}, method = {RequestMethod.POST})
    @ResponseBody
    public CodeMsg examined(@RequestParam("name") String name,
                       HttpServletResponse response) {

        CodeMsg msg = new CodeMsg();
        try{
            msg = fixStaffService.examined(name);
        }catch (Exception e){
            msg.setRetCode(-1);
            msg.setMessage("审核失败！！！");
            logger.error("审核失败！！！"+e);
            return msg;
        }
        return msg;
    }

    /*
     * 新增维护人员
     * */
    @RequestMapping(path = {"/addStaff/"}, method = {RequestMethod.POST})
    @ResponseBody
    public CodeMsg addStaff(@RequestParam("fixnum") int fixnum,
                       @RequestParam("fixname") String fixname,
                       @RequestParam("fixwork") String fixwork,
                       @RequestParam("password") String password,
                       HttpServletResponse response) {

        CodeMsg msg = new CodeMsg();
        FixStaff staff = new FixStaff();
        try{
            staff.setFixnum(fixnum);
            staff.setFixname(fixname);
            staff.setFixwork(fixwork);
            staff.setPassword(password);
            msg = fixStaffService.addStaff(staff);
        }catch (Exception e){
            msg.setRetCode(-1);
            msg.setMessage("新增失败！！！");
            logger.error("新增失败！！！"+e);
            return msg;
        }
        return msg;
    }

    /*
     * 删除维护人员
     * */
    @RequestMapping(path = {"/delStaff/"}, method = {RequestMethod.POST})
    @ResponseBody
    public CodeMsg delStaff(@RequestParam("fixname") String fixname,
                       HttpServletResponse response) {

        CodeMsg msg = new CodeMsg();
        try{
            msg = fixStaffService.delStaff(fixname);
        }catch (Exception e){
            msg.setRetCode(-1);
            msg.setMessage("删除失败！！！");
            logger.error("删除失败！！！"+e);
            return msg;
        }
        return msg;
    }

    /*
     * 问题工单分配给维护人员
     * */
    @RequestMapping(path = {"/fenpeiFix/"}, method = {RequestMethod.POST})
    @ResponseBody
    public CodeMsg fenpeiFix(@RequestParam("fixname") String fixname,
                             @RequestParam("name") String name,
                             @RequestParam("trouble") String trouble,
                            HttpServletResponse response) {

        CodeMsg msg = new CodeMsg();
        try{
            msg = fixStaffService.fenpeiFix(fixname,name,trouble);
        }catch (Exception e){
            msg.setRetCode(-1);
            msg.setMessage("分配失败！！！");
            logger.error("分配失败！！！"+e);
            return msg;
        }
        return msg;
    }

    /*
     * 查看用户反馈
     * */
    @RequestMapping(path = {"/selAssess/"}, method = {RequestMethod.POST})
    @ResponseBody
    public CodeMsg selAssess(
                             HttpServletResponse response) {

        CodeMsg msg = new CodeMsg();
        try{
            msg = fixStaffService.selAssess();
        }catch (Exception e){
            msg.setRetCode(-1);
            msg.setMessage("查询失败！！！");
            logger.error("查询失败！！！"+e);
            return msg;
        }
        return msg;
    }
}
